package net.takela.common.security.model;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.Assert;

import java.io.Serializable;
import java.util.*;

public class AuthUser implements UserDetails {
    private Long uid;
    private Long tenantId;
    private String name;
    private String password;
    private Set<GrantedAuthority> authorities;
    private String username;
    private Boolean accountNonExpired;
    private Boolean accountNonLocked;
    private Boolean credentialsNonExpired;
    private Boolean enabled;
    private List<Integer> roleIds;
    /**
     * 
     */
    public AuthUser(){
        this(null, null, true, true, true, true, new ArrayList(0));
    }
    /**
     * 
     */
    public AuthUser(String username, String password, Collection<? extends GrantedAuthority> authorities) {
        this(username, password, true, true, true, true, authorities);
    }
    /**
     * 
     */
    public AuthUser(String username, String password, boolean enabled, boolean accountNonExpired, boolean credentialsNonExpired, boolean accountNonLocked, Collection<? extends GrantedAuthority> authorities) {
//        Assert.isTrue(username != null && !"".equals(username) && password != null, "Cannot pass null or empty values to constructor");
        this.username = username;
        this.password = password;
        this.enabled = enabled;
        this.accountNonExpired = accountNonExpired;
        this.credentialsNonExpired = credentialsNonExpired;
        this.accountNonLocked = accountNonLocked;
        this.authorities = Collections.unmodifiableSet(sortAuthorities(authorities));
    }
    private static SortedSet<GrantedAuthority> sortAuthorities(Collection<? extends GrantedAuthority> authorities) {
//        Assert.notNull(authorities, "Cannot pass a null GrantedAuthority collection");
        if (authorities  == null){
            return null;
        }
        SortedSet<GrantedAuthority> sortedAuthorities = new TreeSet(new AuthorityComparator());
        Iterator var2 = authorities.iterator();

        while(var2.hasNext()) {
            GrantedAuthority grantedAuthority = (GrantedAuthority)var2.next();
            Assert.notNull(grantedAuthority, "GrantedAuthority list cannot contain any null elements");
            sortedAuthorities.add(grantedAuthority);
        }

        return sortedAuthorities;
    }
    private static class AuthorityComparator implements Comparator<GrantedAuthority>, Serializable {

        private AuthorityComparator() {
        }
    /**
     * 
     */
        public int compare(GrantedAuthority g1, GrantedAuthority g2) {
            if (g2.getAuthority() == null) {
                return -1;
            } else {
                return g1.getAuthority() == null ? 1 : g1.getAuthority().compareTo(g2.getAuthority());
            }
        }
    }
    /**
     * 
     */
    public String getName() {
        return name;
    }
    /**
     * 
     */
    public void setName(String name) {
        this.name = name;
    }
    /**
     * 
     */
    public Long getTenantId() {
        return tenantId;
    }
    /**
     * 
     */
    public void setTenantId(Long tenantId) {
        this.tenantId = tenantId;
    }

    
    /**
     * 
     */
    @Override
    public String getPassword() {
        return password;
    }

    
    /**
     * 
     */
    @Override
    public String getUsername() {
        return username;
    }
    /**
     * 
     */
    public void setUsername(String username) {
        this.username = username;
    }
    /**
     * 
     */
    public void setAuthorities(Collection<? extends GrantedAuthority> authorities) {
        this.authorities = Collections.unmodifiableSet(sortAuthorities(authorities));;
    }

    
    /**
     * 
     */
    @Override
    public Set<GrantedAuthority> getAuthorities() {
        return authorities;
    }

    
    /**
     * 
     */
    @Override
    public boolean isAccountNonExpired() {
        return accountNonExpired;
    }

    
    /**
     * 
     */
    @Override
    public boolean isAccountNonLocked() {
        return accountNonLocked;
    }

    
    /**
     * 
     */
    @Override
    public boolean isCredentialsNonExpired() {
        return credentialsNonExpired;
    }

    
    /**
     * 
     */
    @Override
    public boolean isEnabled() {
        return enabled;
    }
    /**
     * 
     */
    public Long getUid() {
        return uid;
    }
    /**
     * 
     */
    public void setUid(Long uid) {
        this.uid = uid;
    }
    /**
     * 
     */
    public List<Integer> getRoleIds() {
        return roleIds;
    }
    /**
     * 
     */
    public void setRoleIds(List<Integer> roleIds) {
        this.roleIds = roleIds;
    }
}
